<template>
	<BasicModal v-bind="$attrs" z-index="1000" style="z-index:999;" @register="registerModal" title="为上车点选择上车时间"
		@ok="hopOnPointBondingTime" :helpMessage="['提示', '为上车点选择上车时间']">
		<!-- 	<BasicForm @register="registerForm">

		</BasicForm> -->
		<a-row>
			<a-col span="12" v-for="(item,i) in ls" style="margin-bottom:10px;padding-left:10px;">
				<a-row>{{item.title}}</a-row>
				<a-row><a-time-range-picker v-model:value="item.time" format="HH:mm"
						@change="formattingTime(item)" /></a-row>
			</a-col>

		</a-row>

	</BasicModal>
</template>
<script lang="ts" setup>
	import { defineComponent, ref, unref, inject, toRaw } from 'vue';
	import { auditJuneryFormSchema } from '../../CoupleActivityCarpoolBus.data';
	import { useMessage } from '/@/hooks/web/useMessage';
	import dayjs, { Dayjs } from 'dayjs';
	import { hopOnPointBondingTimeApi } from '../../CoupleActivityCarpoolBus.api';
	import { BasicForm, useForm } from '/@/components/Form/index';
	import { BasicModal, useModalInner } from '/@/components/Modal';
	import moment from 'moment';
	const emit = defineEmits(['register', 'successConfirm']);
	const mainId = inject('mainId') || '';
	const obj = ref()
	const obj2 = ref()
	const tiemObj = ref()
	const ls = ref(Array)
	const $message = useMessage()
	let id = '', updateContents, ysLs = []


	function formattingTime(e) {
		 const { time } = e
		 console.log(dayjs(time, 'HH:mm'))
		 
		// console.log('---->', toRaw(time)[1])
		// console.log('---->', unref(time))
		// console.log('---->', e.time[1][0])
		// console.log('---->', e.value)
		// // const { time } = toRaw(e)
		// // console.log('---->',time[0])
		// console.log('------>', moment(toRaw(time)[1], 'HH:mm'))
		// console.log('------>', moment(toRaw(time)[1]).format("HH:mm"))
		 let d1 = "", d2 = "", lss = [];
		// // // 获取小时数和分钟数
		let do1 = new Date(toRaw(time)[0]);
		let hs = prefixZero(do1.getHours())
		let ms = prefixZero(do1.getMinutes())
		d1 = hs + ":" + ms+":00";
		do1 = new Date(toRaw(time)[1]);
		hs = prefixZero(do1.getHours())
		ms = prefixZero(do1.getMinutes())
		d2 = hs + ":" + ms+":00";
		
		for (var i = 0; i < ls.value.length; i++) {
			if (ls.value[i].id == e.id) {
				lss.push(dayjs(d1, 'HH:mm'))
				lss.push(dayjs(d2, 'HH:mm'))
				ls.value[i].time = lss;
			}
		}
		 
		
		

	}
	function prefixZero(num = 0, n = 2) {
		// 数字位数不够，数字前面补零
		return (Array(n).join("0") + num).slice(-n);
	}


	const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
		id = data.record.id
		if (data.record.upCarTimesJson) {
			tiemObj.value = []
			obj.value = data.record.upCarTimesJson
			console.log(obj.value)
			let o = {};
			let os = []
			let d;
			 for (let i = 0; i < obj.value.length; i++) {
				o = {}
				o.id = obj.value[i].id
				o.title = obj.value[i].title
				o.time = [dayjs(obj.value[i].time[0], 'HH:mm'),dayjs(obj.value[i].time[1], 'HH:mm')]
				os.push(o)
			}
			 ls.value = os
		} else {
			tiemObj.value = []
			obj.value = data.record.addressId.split(",")
			obj2.value = data.record.addressId_dictText.split(",")
			let o = {};
			let os = []
			let d;
			for (let i = 0; i < obj.value.length; i++) {
				o = {}
				o.id = obj.value[i]
				o.title = obj2.value[i]
				o.time = tiemObj.value[i]
				os.push(o)
			}

			ls.value = os
		}
		ysLs.value = ls.value

		setModalProps({ zIndex: 1000 })
	})
	const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
		//labelWidth: 150,
		schemas: auditJuneryFormSchema,
		showActionButtonGroup: false,
		baseColProps: { span: 24 }

	});


	async function hopOnPointBondingTime() {
	
		for (let i = 0; i < ls.value.length; i++) {
			if (!ls.value[i].time) {
				return $message.createMessage.warning('有上车点还没选上车时间')
			}
			if (ls.value[i].time != ysLs.value[i].time) {
				updateContents.push(ls.value[i])
			}
		}
		await hopOnPointBondingTimeApi({ upCarTimes: ls.value, id: id, updateContents: updateContents });
		emit('success');
		closeModal();
	}
</script>

<style scoped>
	.scroll-container {
		height: 500px;
	}
</style>